/*  
 * Copyright (c) 2012 Christian Riekoff <info@texone.org>  
 *  
 *  This file is free software: you may copy, redistribute and/or modify it  
 *  under the terms of the GNU General Public License as published by the  
 *  Free Software Foundation, either version 2 of the License, or (at your  
 *  option) any later version.  
 *  
 *  This file is distributed in the hope that it will be useful, but  
 *  WITHOUT ANY WARRANTY; without even the implied warranty of  
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 *  General Public License for more details.  
 *  
 *  You should have received a copy of the GNU General Public License  
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
 *  
 * This file incorporates work covered by the following copyright and  
 * permission notice:  
 */
package cc.creativecomputing.timeline.connector;

import java.awt.Color;
import java.util.Collection;

import cc.creativecomputing.math.CCMath;
import cc.creativecomputing.timeline.controller.TrackController;
import cc.creativecomputing.timeline.model.GroupTrack;
import cc.creativecomputing.timeline.model.Track;
import cc.creativecomputing.timeline.model.TrackType;
import cc.creativecomputing.timeline.model.communication.TypeProperties;
import cc.creativecomputing.timeline.model.points.LinearControlPoint;
import cc.creativecomputing.timeline.model.points.StepControlPoint;

/**
 * @author christianriekoff
 *
 */
public class TimelineConnector {
	public void onChange() {
		Collection<String> myAddresses = _myCommunicationLayer.getObjectNames();
		for (String myAddress : myAddresses) {
			
			String myGroup = myAddress.substring(0, myAddress.lastIndexOf("/"));
			if(!_myColorMap.containsKey(myGroup)) {
				_myColorMap.put(myGroup, Color.getHSBColor(_myHue, 0.75f, 0.75f));
				_myHue += 0.1f;
			}
			if(!_myGroupControllerTrackMap.containsKey(myGroup)) {
				GroupTrack myGroupTrack = new GroupTrack(myGroup);
				myGroupTrack.color(_myColorMap.get(myGroup));
				myGroupTrack.setAddress(myGroup);
				int myIndex = _myTrackCount;
				_myTrackCount++;
				addGroupTrack(myIndex, myGroupTrack);
			}
			
			if(!_myTrackControllerMap.containsKey(myAddress)) {
				
				TypeProperties myProperties = _myCommunicationLayer.getTypeProperties(myAddress);
				
				Track myTrack = new Track(myAddress);
				int myIndex = _myTrackCount;
				_myTrackCount++;
				myTrack.color(_myColorMap.get(myGroup));
				myTrack.trackType(myProperties.trackType());
				myTrack.valueRange(myProperties.minimum(), myProperties.maximum());
				myTrack.accumulateData(myProperties.accumulateValues());
				addCurveTrack(myIndex, myTrack, myGroup);
 			}
		}
		
	}
	
	/**
	 * Reads the values from the communication layer and writes them in to the tracks
	 */
	public void writeValues() {
		for (TrackController myController : _myTrackControllerMap.values()) {
		    double myValue = defaultValue(myController);
				
			if(myController.trackType() == TrackType.DOUBLE) {
				myController.trackData().add(new LinearControlPoint(_myTransportController.time(),myValue));
			}else {
				myController.trackData().add(new StepControlPoint(_myTransportController.time(),myValue));
			}
				
			if(myController.view() != null)myController.view().render();
		}
	}
	

	
	public TimelineConnector communicationLayer() {
		return _myCommunicationLayer;
	}
	

	@Override
	public double defaultValue(TrackController theTrackController) {
		if(_myCommunicationLayer == null) {
    		return 0;
    	}
    	String myAddress = theTrackController.track().address();
		double myValue = _myCommunicationLayer.currentValue(myAddress);
		return CCMath.norm(myValue, theTrackController.track().minValue(),theTrackController.track().maxValue());
	}
}
